<?php

/**
 * Creater : Phuongtt
 * Date : 5/1/2010
 * Idea : Lop xu ly phan quyen NSD
 */
class listxml_permissionController extends  Zend_Controller_Action {
	public function auth(){
        $url = Efy_Init_Config::_getCurrentHttpAndHost();
        if ($_SESSION['STAFF_PERMISSTION'] != 'ADMIN_SYSTEM'){
            $url = $url .'public/error/index/';
            $this->_redirect($url);
        }
    }
	public function init(){
		$this->auth();
		//Load cau hinh thu muc trong file config.ini
        $tempDirApp = Zend_Registry::get('conDirApp');
		$this->_dirApp = $tempDirApp->toArray();
		$this->view->dirApp = $tempDirApp->toArray();
		//Cau hinh cho Zend_layout
		Zend_Layout::startMvc(array(
			    'layoutPath' => $this->_dirApp['layout'],
			    'layout' => 'index'			    
			    ));	
		$response = $this->getResponse();
		
		//Load cau hinh thu muc trong file config.ini de lay ca hang so dung chung
        $tempConstPublic = Zend_Registry::get('ConstPublic');
		$this->_ConstPublic = $tempConstPublic->toArray();
		
		$arrConst = Efy_Init_Const::_setProjectPublicConst();
		$this->view->arrConst = $arrConst;
		//Lay so dong tren man hinh danh sach
		$this->view->NumberRowOnPage 	= $this->_ConstPublic['NumberRowOnPage'];		
		
		//Ky tu dac biet phan tach giua cac phan tu
		$this->view->delimitor 			= $this->_ConstPublic['delimitor'];
		
		//Lay duong dan thu muc goc (path directory root)
		$this->view->baseUrl = $this->_request->getBaseUrl() . "/public/";	
		//Lay cac hang so su dung trong JS public
		Zend_Loader::loadClass('Efy_Init_Config');
		Zend_Loader::loadClass('listxml_modPermission');
		$objConfig = new Efy_Init_Config();
		$this->view->UrlAjax = $objConfig->_setUrlAjax();	
		$this->view->JSPublicConst = $objConfig->_setJavaScriptPublicVariable();		
		
		// Load tat ca cac file Js va Css
		$this->view->LoadAllFileJsCss = Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js','list/jsList.js,permission.js',',','js') . Efy_Publib_Library::_getAllFileJavaScriptCss('','efy-js/Autocomplete','actb_search.js,common_search.js',',','js');	
		// Ham lay thong tin nguoi dang nhap hien thi tai Lefmenu
		//$this->view->InforStaff = Efy_Publib_Library::_InforStaff();
		
		//Dinh nghia current modul code
		$this->view->currentModulCode = "ADMIN";
		$this->view->currentModulCodeForLeft = "permission";								
		//Hien thi file template
		$response->insert('header', $this->view->renderLayout('header.phtml','./application/views/scripts/'));    //Hien thi header 
		$response->insert('menu', $this->view->renderLayout('menu.phtml','./application/views/scripts/'));    //Hien thi header 		    
        $response->insert('footer', $this->view->renderLayout('footer.phtml','./application/views/scripts/'));  	 //Hien thi footer
        $sGetValueInCookie = Efy_Library::_getCookie("showHideMenu");
		if ($sGetValueInCookie == "" || is_null($sGetValueInCookie) || !isset($sGetValueInCookie)){
			Efy_Library::_createCookie("showHideMenu",1);
			$this->view->hideDisplayMeneLeft = 1;
		}else{//Da ton tai Cookie
			if ($sGetValueInCookie != 0){
				$this->view->hideDisplayMeneLeft = 1;
			}else{
				$this->view->hideDisplayMeneLeft = "";
			}
		}       		
	}
	
	/**
	 * Creater : PHUONGTT
	 * Date : 17/10/2011
	 * Idea : Hien thin danh sach NSD cho ban quyen
	 *
	 */
	public function indexAction(){	 	
		//Goi cac doi tuong
		$objInitConfig 			 	= new Efy_Init_Config();
		$objFunction	     		= new Efy_Function_RecordFunctions();	
		$objModel					= new listxml_modPermission();
		// Tao doi tuong Zend_Filter
		//var_dump($_SESSION['arrStaffPermission']);
		$filter = new Zend_Filter();
		//Tieu de man hinh danh sach
		$this->view->titleBody = 'DANH SÁCH THÀNH VIÊN';
		$arrInput = $this->_request->getParams();

		$sFullTextSearch = $this->_request->getParam('txtFullTextSearch');
		//lay thong tin fong a	
		$strFilter = $this->_request->getParam('hdn_filter','');
		if ($strFilter){
			$arrFilter = array();
			$arrFilterTemp = explode('~!@#', $strFilter);
			foreach ($arrFilterTemp as $value){
				$arrValue = explode('~!@', $value);
				$arrFilter = array_merge($arrFilter, array($arrValue[0]=>$arrValue[1]));
			}
			$sFullTextSearch = $arrFilter['sFullTextSearch'];
		}
		$this->view->sFullTextSearch = $sFullTextSearch;
		/** Remember search options */
		$sendSource ='';
		$this->view->strFilter = 'sendSource~!@'.$sendSource
		.'~!@#sFullTextSearch~!@'.$sFullTextSearch;
		$arrResult = $objModel->USERStaffGetAllBySearch($sFullTextSearch);
		$this->view->arrResult = $arrResult;
		$this->view->ownerCode = $sOwner_code;		
	}
	/**
	 * Creater: HUNGVM
	 * Date: 16/09/2009
	 * Idea: Thuc hien phuong thuc Action cap nhat quyen NSD
	 */
	public function addAction(){
		$this->view->bodyTitle = 'CẬP NHẬT QUYỀN THÀNH VIÊN';
		//Tao doi tuong Efy_lib
		$ojbEfyLib = new Efy_Library();		
		// Thuc hien lay du lieu tu form 		
		if($this->_request->isPost()){				
			// Lay toan bo tham so truyen tu form			
			$arrInput = $this->_request->getParams();
			$objPermission = new listxml_modPermission();

			$psFilterXmlTagList = $this->_request->getParam('hdn_filter_xml_tag_list',"");	
			$this->view->filterXmlTagList = $psFilterXmlTagList;
			$psFilterXmlValueList = $this->_request->getParam('hdn_filter_xml_value_list',"");
			$this->view->filterXmlValueList = $psFilterXmlValueList;

			//Lay thong tin quy dinh so row / page
			$piNumRowOnPage = $this->_request->getParam('hdn_record_number_page',0);
			$this->view->numRowOnPage	= $piNumRowOnPage;
			$strFilter = $this->_request->getParam('hdn_filter_to_view','');
			if (!$strFilter)
				$strFilter = $this->_request->getParam('hdn_filter','');
			$this->view->strFilter = $strFilter;
			//Lay danh sach ID can bo duoc chon
			$iStaffIdList = $this->_request->getParam('hdn_object_id_list',"");
			$this->view->iStaffIdList = $iStaffIdList;	
			$this->view->staffInformation = $this->getStaffInformation($iStaffIdList);	
			//Lay danh sach ID quyen
			$userPermissionIdList = $this->_request->getParam('hdn_permission_id_list',"");
			$this->view->userPermissionIdList = $userPermissionIdList;
			//Lay danh mục nhom nguoi dung
			$arrPermissionGroup = Efy_Function_RecordFunctions::getAllObjectbyListCode($_SESSION['OWNER_CODE'],'DM_NHOM_NGUOI_DUNG');
			$this->view->arrPermissionGroup = $arrPermissionGroup;
			//Mang luu tham so update in database
			$arrParameter = array(	'FK_STAFF_ID_LIST'					=>$iStaffIdList,
									'GROUP'								=>$arrInput['group']
							);
			$arrResult = "";
			if (isset($_REQUEST['btn_update']) || $this->_request->getParam('hdn_is_update',"")){		
				$arrResult = $objPermission->StaffPermissionUpdate($arrParameter);	
				$this->_redirect('listxml/permission/index/');
				exit;
			}
		}
	}
	public function deleteAction(){		
		$objPermission = new listxml_modPermission();
		//Lay danh sach ID can bo duoc chon
		$iStaffIdList = $this->_request->getParam('hdn_object_id_list',"");
		$arrResult = $objPermission->StaffPermissionDelete($iStaffIdList);	
		$this->_redirect('listxml/permission/index/');
		exit;
	}
	/**
	 * Creater : HUNGVM
	 * Date : 17/09/2009
	 * Idea : Tao phuong thuc hien thi thong tin NSD da chon de ban quyen
	 *
	 * @param unknown_type $sStaffIdList
	 */
	private function getStaffInformation($sStaffIdList){
		//Sinh Header		
		$sStrHtml = "<table cellpadding='0' cellspacing='0' border='0' align='center' class='list-table-data' id='table-data'>";
			
		$delimitor = "!~~!";
		//Hien thi cac cot cua bang hien thi du lieu
		$strHeaer = Efy_Library::_generateHeaderTable("5%" . $delimitor . "40%" . $delimitor . "20%" . $delimitor . "35%"
											,"TT" . $delimitor . "Tên thành viên" . $delimitor . "Tên đăng nhập" . $delimitor . "Nhóm quyền"
											,$delimitor);
		$StrHeader = explode("!~~!",$strHeaer);
		$sStrHtml .= $StrHeader[0];
		$sStrHtml .= $StrHeader[1];//Hien thi <col width = 'xx'><...
				
		//Kieu style
		$v_current_style_name = "round_row";	
		if ($sStaffIdList != ""){
			$arrStaff = explode($delimitor,$sStaffIdList);
			$objModel = new listxml_modPermission();
			$arrResult = $objModel->USERStaffGetAllBySearch('');
			$count = sizeof($arrResult);
			for ($index = 0;$index < sizeof($arrStaff);$index++){
				$name = '';
				for ($j = 0;$j < $count;$j++){
					if('{'.$arrStaff[$index].'}'==$arrResult[$j]['PK_NET_ID']){
						$name = $arrResult[$j]['C_FULLNAME'];
						$accname = $arrResult[$index]['C_USERNAME'];
						$role = $arrResult[$index]['C_ROLE'];
						break;
					}
				}
				// su dung style
				if ($v_current_style_name == "odd_row"){
					$v_current_style_name = "round_row";
				}else{
					$v_current_style_name = "odd_row";					
				}				
				$sStrHtml .= "<tr class='$v_current_style_name'>";					
				$sStrHtml .= "<td align='center' class='normal_label'>" . ($index+1) . "</td>";					
				$sStrHtml .= "<td align='left' style='padding-left:3px;padding-right:3px; cursor: pointer;' class='normal_label'>" . $name . "</td>";
				$sStrHtml .= "<td align='left' style='padding-left:3px;padding-right:3px; cursor: pointer;' class='normal_label'>" . $accname . "</td>";
				$sStrHtml .= "<td align='left' style='padding-left:3px;padding-right:3px; cursor: pointer;' class='normal_label'>" . $role . "</td>";
				$sStrHtml .= "</tr>";
			}
		}		
		$sStrHtml .= "</table>";
		return $sStrHtml;
	}
}
?>